<?php

	add_action('admin_menu', 'add_custom_box');
	add_action('save_post', 'save_postdata');

	function add_custom_box() {
		add_meta_box( 'metabox', 'Post Custom Important Fields',  'post_custom_box', 'post', 'normal', 'high');
		add_meta_box( 'metabox', 'Page Top Message',  'page_custom_box', 'page', 'normal', 'high');
		add_meta_box('portfolio_options','Overall Page Type & Options','portfolio_options','page','side','high');
	}
	
	function portfolio_options() {
		global $post;
		$get_template = $post->page_template;

		$columns =  get_post_meta($post->ID, "portfolio_columns", true);
				
		if($columns == ""){	$columns = 3;	}
		if($columns == 1){	$zero = 0;	} else { $zero = 1; }
		
		
		$arrwidth = array(450, 450, 280, 210, 160, 135, 115, 100, 90, 80);
		$arrheight = array(250, 250, 150, 125, 5, 6, 7, 8, 9, 10);
		$arrpad = array(10,35,41,20,24,14,10,9,5,4);
		$arrlimit = array(10,8,9,16,15,18,21,24,27,30);
		
		$width = $arrwidth[$columns-$zero];
		$height = $arrheight[$columns-$zero];
		$padding = $arrpad[$columns-$zero];
		$limit = $arrlimit[$columns-$zero];
		
		
		
		for($i=0; $i<=10; $i++){
			if(get_post_meta($post->ID, "portfolio_thumb_height", true) != $arrheight[$i]){
				$height = get_post_meta($post->ID, "portfolio_thumb_height", true);
			}else{
				$height = 150;
			}
			if(get_post_meta($post->ID, "portfolio_thumb_width", true) != $arrwidth[$i]){
				$width = get_post_meta($post->ID, "portfolio_thumb_width", true);
			}else{
				$width = 280;
			}
			
			if(get_post_meta($post->ID, "portfolio_padding", true) != $arrpad[$i]){
				$padding = get_post_meta($post->ID, "portfolio_padding", true);
			}else{
				$padding = 42;
			}
			
			if(get_post_meta($post->ID, "portfolio_limit", true) != $arrpad[$i]){
				$limit = get_post_meta($post->ID, "portfolio_limit", true);
			}else{
				$limit = 9;
			}			
		}
		
		

		if(get_post_meta($post->ID, "blog_thumb_width", true) != ""){
			$blog_width = get_post_meta($post->ID, "blog_thumb_width", true);
		}else{
			$blog_width = 650;
		}
			
		if(get_post_meta($post->ID, "blog_thumb_height", true) != ""){
			$blog_height = get_post_meta($post->ID, "blog_thumb_height", true);
		}else{
			$blog_height = 250;
		}

		if(get_post_meta($post->ID, "blog_limit", true) != ""){
			$blog_limit = get_post_meta($post->ID, "blog_limit", true);
		}else{
			$blog_limit = 10;
		}	
				
		/*
		$columns_size =  get_post_meta($post->ID, "portfolio_columns", true);
		
		if($columns_size == ""){	$columns_size = 3;	}
		if($columns_size == 1){	$zero = 0;	} else { $zero = 1; }
		
		if($columns_size >= 5){	$padding = 10;	}else{	$padding = 20;	}
		
		$arrwidth = array(960, 470, 300, 220, 170, 150, 120, 110, 90, 80);
		$arrheight = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
		$height = $arrheight[$columns_size-1];
		
		$margin = $total_size - (($culomns - 1) * $padding);
		
		$width =  ($total_size - (($columns_size - $zero) * $padding)) / $columns_size;
		$width = (int)($width/10) * 10;

		$padding = ($total_size - ($width * $columns_size)) / ($columns_size-$zero);

		for($i=0; $i<=10; $i++){
			if(get_post_meta($post->ID, "portfolio_thumb_height", true) != $arrheight[$i]){
				$height = get_post_meta($post->ID, "portfolio_thumb_height", true);
			}else{
				$height = 150;
			}
			if(get_post_meta($post->ID, "portfolio_thumb_width", true) != $arrwidth[$i]){
				$width = get_post_meta($post->ID, "portfolio_thumb_width", true);
			}else{
				$width = 300;
			}
		}
		*/
	?>

	<input type="hidden" id="get_columns" value="<?php echo $columns; ?>" />
	<input type="hidden" id="get_height" value="<?php echo $height; ?>" />
	<input type="hidden" id="get_width" value="<?php echo $width; ?>" />
	<input type="hidden" id="get_padding" value="<?php echo $padding; ?>" />
	<input type="hidden" id="get_limit" value="<?php echo $limit; ?>" />

	<input type="hidden" id="get_blog_height" value="<?php echo $blog_height; ?>" />
	<input type="hidden" id="get_blog_width" value="<?php echo $blog_width; ?>" />
	<input type="hidden" id="get_blog_limit" value="<?php echo $blog_limit; ?>" />
	

	<input type="checkbox" id="template_blog_checker" name="page_template" value="page_blog.php" <?php if ($get_template == "page_blog.php") { echo "checked=checked"; } ?> style="display:none;"/>
	<input type="checkbox" id="template_checker" name="page_template" value="page_portfolio.php" <?php if ($get_template == "page_portfolio.php") { echo "checked=checked"; } ?> style="display:none;"/>
	



	
	
		
	
	<div class="show_portfolio_details" style="<?php if ($get_template != "page_portfolio.php") { echo "display:none;"; } ?>">
		
		
	<div class="misc-pub-section">
		<label for="post_status"><strong>Selected Portfolio & Gallery Page</strong></label>
	</div>
	

	
		<div class="misc-pub-section">
			<div class="meta_title_div">Categories</div>
			<span id="post-status-display">
				<?php
					$current_value =  get_post_meta($post->ID, "portfolio_id", true);
					list_categories("portfolio_id",$current_value, "Join as Category", "portfolio_id");
				?>
			</span>
		</div>


			
		<div class="misc-pub-section">
			<div class="meta_title_div">Columns</div>
			<span id="post-status-display">
				<?php
					$newArry = array(1,2,3,4,5,6,7,8,9,10);
					quick_list("portfolio_columns", $columns, $newArry, "portfolio_columns");
				?>
			</span>
		</div>


		<div class="misc-pub-section">
				<div class="meta_title_div">Thumb. Width</div>
				<div>
				<input type="text" value="<?php if ($get_template == "page_portfolio.php") { echo $width; } ?>" id="portfolio_thumb_width" size="4" name="portfolio_thumb_width"><span class="tablespan">px</span>
				</div>
				
				<div class="meta_title_div">Thumb. Height</div>
				<div>
					<input type="text" value="<?php if ($get_template == "page_portfolio.php") { echo $height; } ?>" id="portfolio_thumb_height" size="4" name="portfolio_thumb_height"><span class="tablespan">px</span>	
				</div>
			
				<div class="meta_title_div">Between</div>
				<div>
					<input type="text" value="<?php if ($get_template == "page_portfolio.php") { echo $padding; } ?>" id="portfolio_padding" size="4" name="portfolio_padding"><span class="tablespan">px</span> 
				</div>
				
				<div class="meta_title_div">Item Limit</div>
				<div>
					<input type="text" value="<?php if ($get_template == "page_portfolio.php") { echo $limit; } ?>" id="portfolio_limit" size="4" name="portfolio_limit"><span class="tablespan">item</span> 
				</div>
		</div>

		
				
		<div class="misc-pub-section">
			<label for="post_status">Disable -> </label>
			<span id="post-status-display">
				Title <input type="checkbox" name="title_text" id="title_text" value="1" <?php if (get_post_meta($post->ID, "title_text", true) == 1) { echo "checked=checked"; } ?>/>
				Detail <input type="checkbox" name="detail_text" id="detail_text" value="1" <?php if (get_post_meta($post->ID, "detail_text", true) == 1) { echo "checked=checked"; } ?>/>
				Desc. <input type="checkbox" name="desc_text" id="desc_text" value="1" <?php if (get_post_meta($post->ID, "desc_text", true) == 1) { echo "checked=checked"; } ?>/>
			</span>
		</div>
		
		
		<div class="misc-pub-section">
			<label for="post_status">Use there <strong>Filter By</strong> -></label>
			<span id="post-status-display">
				<input type="checkbox" name="do_filter" id="do_filter" value="1" <?php if (get_post_meta($post->ID, "do_filter", true) == 1) { echo "checked=checked"; } ?>/>
			</span>
		</div>
		
	<!--
		<div class="misc-pub-section">
			<label for="post_status">with Sidebar:</label>
			<span id="post-status-display">
				<input type="checkbox" name="portfolio_sidebar" id="portfolio_sidebar" value="1" <?php if (get_post_meta($post->ID, "portfolio_sidebar", true) == "1") { echo "checked=checked"; } ?>/>
			</span>
		</div>
	-->
		
		
	
	</div>

	<div class="show_blog_details" style="<?php if ($get_template != "page_blog.php") { echo "display:none;"; } ?>">
		
		<div class="misc-pub-section">
			<label for="post_status"><strong>Selected Blog Page</strong></label>
		</div>
	
		<div class="misc-pub-section">
			<div class="meta_title_div">Category</div>
			<div>
				<?php
					$current_value =  get_post_meta($post->ID, "blog_id", true);
					list_categories("blog_id",$current_value, "Join as Category", "blog_id");
				?>
			</div>
		</div>
		
		<div class="misc-pub-section">
				<div class="meta_title_div">Thumb. Width</div>
				<div>
				<input type="text" value="<?php if ($get_template == "page_blog.php") { echo $blog_width; } ?>" id="blog_thumb_width" size="4" name="blog_thumb_width"><span class="tablespan">px</span>
				</div>
				
				<div class="meta_title_div">Thumb. Height</div>
				<div>
					<input type="text" value="<?php if ($get_template == "page_blog.php") { echo $blog_height; } ?>" id="blog_thumb_height" size="4" name="blog_thumb_height"><span class="tablespan">px</span>	
				</div>
							
				<div class="meta_title_div">Item Limit</div>
				<div>
					<input type="text" value="<?php if ($get_template == "page_blog.php") { echo $blog_limit; } ?>" id="blog_limit" size="4" name="blog_limit"><span class="tablespan">item</span> 
				</div>
		</div>
		
	</div>


	<div class="misc-pub-section info_desc" <?php if ($get_template == "page_blog.php" or $get_template == "page_portfolio.php") { echo "style=\"display:none;\""; } ?>>
		<label for="post_status">Select <strong>template type</strong> from page attributes</label>
	</div>
	
	
	<?php
	}	

	function post_custom_box() {
		global $post;
		
		page_custom_box();
		
		echo '<input type="hidden" name="myplugin_noncename" id="myplugin_noncename" value="' . wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
	
?>
<style>
.one_preview img {
	width:100px;
	height:100px;
}
.one_preview {
	width:100px;
	height:100px;
	padding:5px;
	margin:10px;
	border:1px solid #ececec;
	display:table;
	background-color:#f9f9f9;
}
</style>
<table>

	<tr>
		<td>
			<span id="thumb_one_preview" class="one_preview"><img src="<?php echo get_post_meta($post->ID, "thumb_one", true); ?>"></span>
			<center><a href="media-upload.php?post_id=1&amp;type=image&amp;TB_iframe=true" id="thumb_one" class="set_input thickbox button" title='Add an Image' onclick="return false;">Upload Picture</a></center>
		</td>
		<td style="width:100%;">Thumbnail Image Link (URL): <br /><br /><input type="text" name="thumb_one" id="thumb_one" class="upload_input" tabindex="1" value="<?php echo get_post_meta($post->ID, "thumb_one", true); ?>" style="width:100%;"/></td>
	</tr>
	
	<tr>
		<td colspan="2"><br /><br /></td>
	</tr>
	
	
	<tr>
		<td>
			<span id="big_one_preview" class="one_preview"><img src="<?php echo get_post_meta($post->ID, "big_one", true); ?>"></span>
			<center><a href="media-upload.php?post_id=1&amp;type=image&amp;TB_iframe=true" id="big_one" class="set_input thickbox button" title='Add an Image' onclick="return false;">Upload Picture</a></center>
		</td>
		<td style="width:100%;">FullSize Image Link (URL): <br /><br /><input type="text" name="big_one" id="big_one" class="upload_input" tabindex="1" value="<?php echo get_post_meta($post->ID, "big_one", true); ?>" style="width:100%;"/></td>
	</tr>
		<tr>
		<td colspan="2"><br /><br /></td>
	</tr>

	<tr>
		<td colspan="2">
			Short Desription:<br /><br />
		</td>
	</tr>
	
	<tr>
		<td colspan="2">
			<textarea cols=10 row=10 name="short_desc" id="short_desc" style="width:99%"><?php echo get_post_meta($post->ID, "short_desc", true); ?></textarea>
		</td>
	</tr>
	
	<tr>
		<td colspan="2"><br /><br /></td>
	</tr>
		<tr>
		<td colspan="2">
			Video Embed <font size="1" color="#ccc"><i>(if you need to add video)</i>:<br /><br />
		</td>
	</tr>
	
	<tr>
		<td colspan="2">
			<textarea name="video_one" id="video_one" style="width:99%; height:125px;"><?php echo get_post_meta($post->ID, "video_one", true); ?></textarea>
			<br/><br/>
			<font size="1" color="#ccc"><i>(Please, do not forget to set <strong>width</strong> & <strong>height</strong>)</i>
		</td>
	</tr>
	

	

</table>

<?php
}
	
	function page_custom_box() {
		global $post;
		echo '<input type="hidden" name="myplugin_noncename" id="myplugin_noncename" value="' . wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
		echo '<label for="myplugin_new_field">Page Top Message</label> ';
		$current_value =  get_post_meta($post->ID, "top_title", true);
		echo "<input type=\"text\" style=\"width:75%\" name=\"top_title\" value=\"".$current_value."\" />";
	}
	
	
	
	function save_postdata( $post_id ) {

	if ( !wp_verify_nonce( $_POST['myplugin_noncename'], plugin_basename(__FILE__) )) {
    	return $post_id;
  	}

  	if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) 
    	return $post_id;
	
	if ( 'page' == $_POST['post_type'] ) {
    	if ( !current_user_can( 'edit_page', $post_id ) )
     		 return $post_id;
  	} else {
    	if ( !current_user_can( 'edit_post', $post_id ) )
      		return $post_id;
  	}
	
	if ( $parent_id = wp_is_post_revision($post_id) )
	{
		$post_id = $parent_id;
	}
	
	
	if (!get_post_meta($post_id, "thumb_one")) {
		add_post_meta($post_id, "thumb_one", $_POST["thumb_one"]);
  	}else{
  		update_post_meta($post_id, "thumb_one", $_POST["thumb_one"]);
  	}
	if ($_POST["thumb_one"] == "") {
		  delete_post_meta($post_id, "thumb_one");
	}

  	if (!get_post_meta($post_id, "big_one")) {
		add_post_meta($post_id, "big_one", $_POST["big_one"]);
  	}else{
  		update_post_meta($post_id, "big_one", $_POST["big_one"]);
  	}
	if ($_POST["big_one"] == "") {
		  delete_post_meta($post_id, "big_one");
	}
	
	if (!get_post_meta($post_id, "video_one")) {
		add_post_meta($post_id, "video_one", $_POST["video_one"]);
  	}else{
  		update_post_meta($post_id, "video_one", $_POST["video_one"]);
  	}
	if ($_POST["video_one"] == "") {
		  delete_post_meta($post_id, "video_one");
	}

  	if (!get_post_meta($post_id, "short_desc")) {
		add_post_meta($post_id, "short_desc", $_POST["short_desc"]);
  	}else{
  		update_post_meta($post_id, "short_desc", $_POST["short_desc"]);
  	}
	if ($_POST["short_desc"] == "") {
		  delete_post_meta($post_id, "short_desc");
	}
	


	if (!get_post_meta($post_id, "top_title")) {
		add_post_meta($post_id, "top_title", $_POST["top_title"]);
  	}else{
  		update_post_meta($post_id, "top_title", $_POST["top_title"]);
  	}
	if ($_POST["top_title"] == "") {
		  delete_post_meta($post_id, "top_title");
	}
	
	if (!get_post_meta($post_id, "portfolio_id")) {
		add_post_meta($post_id, "portfolio_id", $_POST["portfolio_id"]);
  	}else{
  		update_post_meta($post_id, "portfolio_id", $_POST["portfolio_id"]);
  	}
	if ($_POST["portfolio_id"] == "") {
		  delete_post_meta($post_id, "portfolio_id");
	}
	/*
	if (!get_post_meta($post_id, "portfolio_sidebar")) {
		add_post_meta($post_id, "portfolio_sidebar", $_POST["portfolio_sidebar"]);
  	}else{
  		update_post_meta($post_id, "portfolio_sidebar", $_POST["portfolio_sidebar"]);
  	}
	if ($_POST["portfolio_sidebar"] == "") {
		  delete_post_meta($post_id, "portfolio_sidebar");
	}
	*/

	if (!get_post_meta($post_id, "portfolio_columns")) {
		add_post_meta($post_id, "portfolio_columns", $_POST["portfolio_columns"]);
  	}else{
  		update_post_meta($post_id, "portfolio_columns", $_POST["portfolio_columns"]);
  	}
	if ($_POST["portfolio_columns"] == "") {
		  delete_post_meta($post_id, "portfolio_columns");
	}	
	
	



	if (!get_post_meta($post_id, "portfolio_thumb_width")) {
		add_post_meta($post_id, "portfolio_thumb_width", $_POST["portfolio_thumb_width"]);
  	}else{
  		update_post_meta($post_id, "portfolio_thumb_width", $_POST["portfolio_thumb_width"]);
  	}
	if ($_POST["portfolio_thumb_width"] == "") {
		  delete_post_meta($post_id, "portfolio_thumb_width");
	}


	if (!get_post_meta($post_id, "portfolio_padding")) {
		add_post_meta($post_id, "portfolio_padding", $_POST["portfolio_padding"]);
  	}else{
  		update_post_meta($post_id, "portfolio_padding", $_POST["portfolio_padding"]);
  	}
	if ($_POST["portfolio_padding"] == "") {
		  delete_post_meta($post_id, "portfolio_padding");
	}		
	
	
	
	if (!get_post_meta($post_id, "portfolio_limit")) {
		add_post_meta($post_id, "portfolio_limit", $_POST["portfolio_limit"]);
  	}else{
  		update_post_meta($post_id, "portfolio_limit", $_POST["portfolio_limit"]);
  	}
	if ($_POST["portfolio_limit"] == "") {
		  delete_post_meta($post_id, "portfolio_limit");
	}	


/*new blog*/	
	if (!get_post_meta($post_id, "blog_thumb_width")) {
		add_post_meta($post_id, "blog_thumb_width", $_POST["blog_thumb_width"]);
  	}else{
  		update_post_meta($post_id, "blog_thumb_width", $_POST["blog_thumb_width"]);
  	}
	if ($_POST["blog_thumb_width"] == "") {
		  delete_post_meta($post_id, "blog_thumb_width");
	}	

	if (!get_post_meta($post_id, "blog_thumb_height")) {
		add_post_meta($post_id, "blog_thumb_height", $_POST["blog_thumb_height"]);
  	}else{
  		update_post_meta($post_id, "blog_thumb_height", $_POST["blog_thumb_height"]);
  	}
	if ($_POST["blog_thumb_height"] == "") {
		  delete_post_meta($post_id, "blog_thumb_height");
	}		
	
	if (!get_post_meta($post_id, "blog_limit")) {
		add_post_meta($post_id, "blog_limit", $_POST["blog_limit"]);
  	}else{
  		update_post_meta($post_id, "blog_limit", $_POST["blog_limit"]);
  	}
	if ($_POST["blog_limit"] == "") {
		  delete_post_meta($post_id, "blog_limit");
	}	




	if (!get_post_meta($post_id, "portfolio_thumb_height")) {
		add_post_meta($post_id, "portfolio_thumb_height", $_POST["portfolio_thumb_height"]);
  	}else{
  		update_post_meta($post_id, "portfolio_thumb_height", $_POST["portfolio_thumb_height"]);
  	}
	if ($_POST["portfolio_thumb_height"] == "") {
		  delete_post_meta($post_id, "portfolio_thumb_height");
	}	
	
	if (!get_post_meta($post_id, "title_text")) {
		add_post_meta($post_id, "title_text", $_POST["title_text"]);
  	}else{
  		update_post_meta($post_id, "title_text", $_POST["title_text"]);
  	}
	if ($_POST["title_text"] == "") {
		  delete_post_meta($post_id, "title_text");
	}	


	if (!get_post_meta($post_id, "detail_text")) {
		add_post_meta($post_id, "detail_text", $_POST["detail_text"]);
  	}else{
  		update_post_meta($post_id, "detail_text", $_POST["detail_text"]);
  	}
	if ($_POST["detail_text"] == "") {
		  delete_post_meta($post_id, "detail_text");
	}		
	

	if (!get_post_meta($post_id, "desc_text")) {
		add_post_meta($post_id, "desc_text", $_POST["desc_text"]);
  	}else{
  		update_post_meta($post_id, "desc_text", $_POST["desc_text"]);
  	}
	if ($_POST["desc_text"] == "") {
		  delete_post_meta($post_id, "desc_text");
	}		

	
	if (!get_post_meta($post_id, "do_filter")) {
		add_post_meta($post_id, "do_filter", $_POST["do_filter"]);
  	}else{
  		update_post_meta($post_id, "do_filter", $_POST["do_filter"]);
  	}
	if ($_POST["do_filter"] == "") {
		  delete_post_meta($post_id, "do_filter");
	}	
	
		
		
	if (!get_post_meta($post_id, "blog_id")) {
		add_post_meta($post_id, "blog_id", $_POST["blog_id"]);
  	}else{
  		update_post_meta($post_id, "blog_id", $_POST["blog_id"]);
  	}
	if ($_POST["blog_id"] == "") {
		  delete_post_meta($post_id, "blog_id");
	}	
	
	
}